bitmasks brute force constructive algorithms dp math *1500

Please click on ads to support us..

Python Code:

fact = []
i = 2
x = 1
while x < 1e13:
    fact.append(x)
    x *= i
    i += 1
l = len(fact) def main():
    
    t = int(input())
    allans = []
    for _ in range(t):
        n = int(input())
                ans = 1000
        if bin(n).count('1') == 1:             ans = 1
        for mask in range(1 << l):
            total = 0
            for i in range(l):
                if (mask & (1 << i)) > 0:
                    total += fact[i]
            if total <= n:
                cnts = bin(mask).count('1') + bin((n - total)).count('1')
                ans = min(ans, cnts)
            else:
                break
        allans.append(ans)
    multiLineArrayPrint(allans)
    
    return
 
import sys
input=sys.stdin.buffer.readline  
def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
 
def readIntArr():
    return [int(x) for x in input().split()]
 
def makeArr(defaultValFactory,dimensionArr):     dv=defaultValFactory;da=dimensionArr
    if len(da)==1:return [dv() for _ in range(da[0])]
    else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
 
def queryInteractive(a, b, c):
    print('? {} {} {}'.format(a, b, c))
    sys.stdout.flush()
    return int(input())
 
def answerInteractive(x1, x2):
    print('! {} {}'.format(x1, x2))
    sys.stdout.flush()
 
inf=float('inf')
 
from math import gcd,floor,ceil
import math
 
for _abc in range(1):
    main()

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long 

ll fac[15];
ll minimum;
ll counter = 0;

void generate(ll n, ll largest)
{
	if(largest == 0)
	{
		if(n < 0) return;
		int b = 0;
		for(ll i = 1; i <= n; i=(i<<1)) if(n&i){b++;}
		counter+=b;
		minimum = min(minimum, counter);
		counter-=b;
	}
	else
	{
		if(n < 0) return;
		generate(n, largest - 1);
		counter++;
		generate(n - fac[largest], largest - 1);
		counter--;
	}
}

ll solve()
{
	ll n; cin >> n;
	ll largest_fac = (upper_bound(fac, fac + 15, n) - fac) - 1;
	minimum = LLONG_MAX;
	generate(n, largest_fac);
	return minimum;
}

int main()
{
	fac[1] = 1;
	for(ll i = 2; i < 15; i++)
	{
		fac[i] = fac[i-1] * i;
	}

	int t; cin >> t; while(t--) cout << solve() << "\n";
}


Comments

Submit
0 Comments
More Questions

911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes
1005B - Delete from the Left
94A - Restoring Password
1529B - Sifid and Strange Subsequences
1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments
1428B - Belted Rooms
519B - A and B and Compilation Errors
1152B - Neko Performs Cat Furrier Transform
1411A - In-game Chat
119A - Epic Game
703A - Mishka and Game
1504C - Balance the Bits
988A - Diverse Team
1312B - Bogosort
1616B - Mirror in the String